<template>
  <div class="ZJKPZ">
    <div class="query">
      <el-button
        size="small"
        icon="el-icon-plus"
        type="success"
        @click="dialogVisible = true"
      ></el-button>
    </div>
    <div class="main" style="margin-top: 10px">
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        height="650"
        v-loading="isLoading"
      >
        <el-table-column prop="name" label="数据库名称" width="180">
        </el-table-column>
        <el-table-column prop="code" label="数据库类型" width="180">
        </el-table-column>
        <el-table-column prop="connstr" label="连接字符串"> </el-table-column>
        <el-table-column prop="remark" label="备注"> </el-table-column>
        <el-table-column prop="collectHour" label="每日采集数据时间点(小时)">
        </el-table-column>
        <el-table-column prop="collectMinute" label="每日采集数据时间点(分钟)">
        </el-table-column>
        <el-table-column label="操作" width="140" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              icon="el-icon-edit"
              @click="edit(scope.row)"
              type="success"
            ></el-button>
            <el-button
              size="mini"
              icon="el-icon-delete"
              @click="del(scope.row.id)"
              type="danger"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page" style="margin-top: 5px">
      <span style="float: left">总共 {{ total }} 条记录</span>
      <el-pagination
        background
        layout="prev, pager, next"
        :page-size="query.pageSize"
        :current-page="query.currentPage"
        :total="total"
        @current-change="page_change"
      >
      </el-pagination>
    </div>
    <!-- 弹层 -->

    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="45%"
      :before-close="handleClose"
      :append-to-body="true"
      :close-on-click-modal="false"
    >
      <el-form
        label-position="right"
        label-width="180px"
        :model="formLabelAlign"
      >
        <el-form-item
          label="数据库名称"
          style="display: inline-block; width: 100%"
        >
          <el-input v-model="formLabelAlign.name"></el-input>
        </el-form-item>
        <el-form-item
          label="数据库类型"
          style="display: inline-block; width: 100%"
        >
          <el-select
            v-model="formLabelAlign.code"
            placeholder="请选择数据库类型"
            style="width: 100%"
          >
            <el-option label="Oracle" value="Oracle"> </el-option>
            <el-option label="SqlServer" value="SqlServer"> </el-option>
            <el-option label="MySql" value="MySql"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="连接字符串"
          style="display: inline-block; width: 100%"
        >
          <el-input v-model="formLabelAlign.connstr"></el-input>
        </el-form-item>
        <el-form-item label="备注" style="display: inline-block; width: 100%">
          <el-input v-model="formLabelAlign.remark"></el-input>
        </el-form-item>
        <el-form-item
          label="每日采集数据时间点(小时)"
          style="display: inline-block; width: 100%"
        >
          <el-input
            type="number"
            v-model="formLabelAlign.collectHour"
            placeholder="请填写数字，默认为 0 点"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="每日采集数据时间点(分钟)"
          style="display: inline-block; width: 100%"
        >
          <el-input
            type="number"
            v-model="formLabelAlign.collectMinute"
            placeholder="请填写数字，默认为 30 分"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="btn_OK">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  // 组件名称
  name: "",
  // 局部注册的组件
  components: {},
  // 组件参数 接收来自父组件的数据
  props: {},
  // 组件状态值
  data() {
    return {
      tableData: [],
      total: 0,
      title: "添加",
      dialogVisible: false,
      formLabelAlign: {},
      query: {
        pageSize: 15,
        currentPage: 1,
      },
      isLoading: false,
    };
  },
  // 计算属性
  computed: {},
  // 侦听器
  watch: {},
  // 生命周期钩子   注：没用到的钩子请自行删除
  /**
   * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
   */
  created() {},
  /**
   * el 被新创建的 vm.el 替换，并挂载到实例上去之后调用该钩子。
   * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.el 也在文档内。
   */
  mounted() {
    this.getTableList();
  },
  // 组件方法
  methods: {
    getTableList() {
      this.isLoading = true;
      this.$post(
        baseUrl.jbIp + "/T1/api/v1/Platform/Setting/GetListDB",
        this.query
      ).then((res) => {
        this.tableData = res.data.list;
        this.total = res.data.total;
        this.isLoading = false;
      });
    },
    page_change(val) {
      this.query.currentPage = val;
      this.getTableList();
    },
    handleClose() {
      this.dialogVisible = false;
      this.formLabelAlign = {};
      this.title = "添加";
    },
    btn_OK() {
      if (!this.formLabelAlign.id) {
        this.$post(
          baseUrl.jbIp + "/T1/api/v1/Platform/Setting/CreateDB",
          this.formLabelAlign
        ).then((res) => {
          if (res.code != 200) {
            this.$message.error(res.message);

            return;
          }
          this.getTableList();
          this.$message({
            message: "操作成功",
            type: "success",
          });
          this.handleClose();
        });
      } else {
        this.$put(
          baseUrl.jbIp + "/T1/api/v1/Platform/Setting/UpdateDB",
          this.formLabelAlign
        ).then((res) => {
          if (res.code != 200) {
            this.$message.error(res.message);

            return;
          }
          this.getTableList();
          this.$message({
            message: "操作成功",
            type: "success",
          });
          this.handleClose();
        });
      }
    },
    del(id) {
      this.$deleteF(
        baseUrl.jbIp + "/T1/api/v1/Platform/Setting/DeleteDB?id=" + id
      ).then((res) => {
        if (res.code != 200) {
          this.$message.error(res.message);

          return;
        }
        this.$message({
          message: "已删除",
          type: "success",
        });
        this.getTableList();
      });
    },
    edit(val) {
      this.title = "修改";
      var init = JSON.stringify(val);
      this.formLabelAlign = JSON.parse(init);
      this.dialogVisible = true;
    },
  },
};
</script>

<style scoped lang="scss">
.ZJKPZ {
  padding: 20px;
}
</style>
